package com.yeyks.app.controller.base.activity;

import cn.hutool.extra.servlet.ServletUtil;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.append.activity.ActivityZhouJieLunService;
import com.yeyks.commonReference.pojo.param.activity.ZhouJieLun.ActivityZhouJieLunLeaderboardDetailsParam;
import com.yeyks.commonReference.pojo.param.activity.ZhouJieLun.ActivityZhouJieLunLotteryParam;
import com.yeyks.commonReference.pojo.param.activity.ZhouJieLun.ActivityZhouJieLunStatisticIncrementParam;
import com.yeyks.commonReference.pojo.vo.activity.ZhouJieLun.ActivityZhouJieLunLeaderboardDetailsVo;
import com.yeyks.commonReference.pojo.vo.activity.ZhouJieLun.ActivityZhouJieLunLotteryVo;
import com.yeyks.consumer.param.UserInfo.UserInfoPhoneLoginParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

//@Api(tags = "周杰伦活动", description = "周杰伦活动")
//@Controller
@RequestMapping("/activity/zhoujielun")
public class ActivityZhouJieLunController {

    @Autowired
    private ActivityZhouJieLunService activityZhouJieLunService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String deathlock = "statistical:activity:zhoujielun:health:lock";

    //统计
    @ApiOperation(value = "统计v3.6.2", notes = "统计")
    @PostMapping("/statistic/increment")
    @ResponseBody
    public AjaxResponseBody<Boolean> statisticIncrement(@Validated ActivityZhouJieLunStatisticIncrementParam param) {
        return AjaxResponseBody.success(activityZhouJieLunService.statisticIncrement(param));
    }

    //抢票
    @ApiOperation(value = "抢票v3.6.2", notes = "抢票")
    @PostMapping("/register")
    @ResponseBody
    public AjaxResponseBody<Boolean> register(UserInfoPhoneLoginParam param, HttpServletRequest request) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        Long increment = hashOperations.increment(deathlock, param.getUserId() + "_" + param.getPhone() + "_" + ServletUtil.getClientIP(request), 1);
        if (increment > 10) {
            try {
                redisTemplate.delete(param.getPhone());
            } catch (Exception e) {
            }
            throw new ServiceException("未知错误");
        }
        return AjaxResponseBody.success(activityZhouJieLunService.register(param));
    }

    //详情列表
    @ApiOperation(value = "详情列表v3.6.2", notes = "详情列表")
    @PostMapping("/leaderboardDetails")
    @ResponseBody
    public AjaxResponseBody<ActivityZhouJieLunLeaderboardDetailsVo> leaderboardDetails(ActivityZhouJieLunLeaderboardDetailsParam param) {
        return AjaxResponseBody.success(activityZhouJieLunService.leaderboardDetails(param));
    }

    //发送验证码
    @ApiOperation(value = "发送验证码v3.6.2", notes = "发送验证码")
    @PostMapping("/sendCode")
    @ResponseBody
    public AjaxResponseBody<Boolean> sendCode(String phone) {
        activityZhouJieLunService.sendCode(phone);
        return AjaxResponseBody.success();
    }

    //抽奖
    @ApiOperation(value = "抽奖v3.6.2", notes = "抽奖")
    @PostMapping("/lottery")
    @ResponseBody
    public AjaxResponseBody<ActivityZhouJieLunLotteryVo> lottery(@Validated ActivityZhouJieLunLotteryParam param) {
        return AjaxResponseBody.success(activityZhouJieLunService.lottery(param));
    }

}
